﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Kho_PhieuNhap : Lib.Bases.BaseClass
    {
        Kho_PhieuNhap_ChiTiet _ctiet = new Kho_PhieuNhap_ChiTiet();

        #region Prop
        public System.Int32 IdPhieuNhap
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.String So
        {
            get;
            set;
        }

        public System.Int32 IdKhachHang
        {
            get;
            set;
        }

        public System.String NguoiNhap
        {
            get;
            set;
        }

        public System.String DienGiai
        {
            get;
            set;
        }

        #endregion

        public DateTime F_FromDate { get; set; }
        public DateTime F_ToDate { get; set; }
        public bool F_Detail { get; set; }

        public DataTable Detail { get; set; }

        #region  Constructors
        public Kho_PhieuNhap()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #endregion

        #region Methods
        public override void ResetData()
        {
            IdPhieuNhap = 0;
            Ngay = DateTime.Now;
            So = string.Empty;
            IdKhachHang = 0;
            NguoiNhap = string.Empty;
            DienGiai = string.Empty;
        }

        public void ReadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Kho_PhieuNhap", "Command,IdPhieuNhap", "ReadData", IdPhieuNhap);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string GetNo(DateTime Date)
        {
            return Cvert.ToString(m_DataLayer.ExecScalar("SP_Kho_PhieuNhap", "Command,Ngay", "GetNo", Cvert.ToShortDate(Date)), "");
        }

        public override DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_PhieuNhap", "Command,TuNgay,DenNgay,F_Detail", "LoadData", Cvert.ToShortDate(F_FromDate), Cvert.ToShortDate(F_ToDate), F_Detail);
        }

        public DataTable GetDataPrint()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_PhieuNhap", "Command,IdPhieuNhap", "GetDataPrint", IdPhieuNhap);
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdPhieuNhap"))
                IdPhieuNhap = Lib.Data.ValueType.GetValue(row["IdPhieuNhap"], IdPhieuNhap);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("So"))
                So = Lib.Data.ValueType.GetValue(row["So"], So);
            if (row.Table.Columns.Contains("IdKhachHang"))
                IdKhachHang = Lib.Data.ValueType.GetValue(row["IdKhachHang"], IdKhachHang);
            if (row.Table.Columns.Contains("NguoiNhap"))
                NguoiNhap = Lib.Data.ValueType.GetValue(row["NguoiNhap"], NguoiNhap);
            if (row.Table.Columns.Contains("DienGiai"))
                DienGiai = Lib.Data.ValueType.GetValue(row["DienGiai"], DienGiai);
        }

        public DataTable Get_ChiTiet()
        {
            return _ctiet.GetDetail(m_DataLayer, IdPhieuNhap);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdPhieuNhap", IdPhieuNhap);
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("So", So);
                Lib.Data.ParamHolder.AddParam("IdKhachHang", IdKhachHang);
                Lib.Data.ParamHolder.AddParam("NguoiNhap", NguoiNhap);
                Lib.Data.ParamHolder.AddParam("DienGiai", DienGiai);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_KHO_PHIEUNHAP", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdPhieuNhap = Cvert.ToInt32(output["IdPhieuNhap"], 0);

                _ctiet.OnDelete(m_DataLayer, IdPhieuNhap);
                _ctiet.Insert(m_DataLayer, Detail, IdPhieuNhap);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                _ctiet.OnDelete(m_DataLayer, IdPhieuNhap);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdPhieuNhap", IdPhieuNhap);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_KHO_PHIEUNHAP", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }
        #endregion
    }
}
